package com.spring.security.controller;


import com.spring.security.dto.User;
import com.spring.security.social.AppSingUpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.social.connect.web.ProviderSignInUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.ServletWebRequest;

import javax.servlet.http.HttpServletRequest;

@RestController
public class HelloController {

    @GetMapping("hello")
    public String hello() {
        return "hello spring security";
    }

    @GetMapping("index")
    public String index() {
        return "登录成功跳转";
    }


    @GetMapping("failure")
    public String failure() {
        return "登录失败跳转";
    }

    @Autowired
    private ProviderSignInUtils providerSignInUtils;

    @Autowired
    private AppSingUpUtils appSingUpUtils;

    /**
     * 注册和绑定
     *
     * @return
     */
    @PostMapping("/user/regist")
    public void regist(User user, HttpServletRequest request) {
        //这里不管是绑定还是添加  我们都会通过查询或者添加返回用户ID 唯一标
        //我先用用户名当成唯一标识
        String userId = user.getUsername();

        appSingUpUtils.doPostSignUp(new ServletWebRequest(request), userId);
    }

    /**
     * 获取用户资料
     *
     * @param user
     * @return
     */
    @GetMapping("/user/me")
    private Object getCurrentUser(@AuthenticationPrincipal UserDetails user) {
        return user;
    }
}
